package com.dongqi.myblog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongqi.myblog.po.TBlog;
import com.dongqi.myblog.po.TBlogTag;
import com.dongqi.myblog.po.TComment;
import com.dongqi.myblog.po.TUser;
import com.dongqi.myblog.po.vo.BlogQuery;
import com.dongqi.myblog.service.*;
import com.dongqi.myblog.util.MyStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongqi
 * @since 2020-08-03
 */
@Controller
@RequestMapping("/admin")
public class BlogsController {

    private static final String INPUT="admin/blogs-input";
    private static final String LIST="admin/blogs";
    private static final String REDIRECT_LIST="redirect:/admin/blogs";

    @Autowired
    private TBlogService blogService;
    @Autowired
    private TTypeService typeService;
    @Autowired
    private TTagService tagService;
    @Autowired
    private TBlogTagService blogTagService;
    @Autowired
    private OssService ossService;

    @GetMapping("/blogs")
    public String blogs(@RequestParam(defaultValue = "1") Long pageNo,
                        @RequestParam(defaultValue = "10") Long pageSize,
                        TBlog blog,
                        Model model){
        Page<BlogQuery> page = new Page<>(pageNo,pageSize);
        Page<BlogQuery> page1 = blogService.listBlogs(blog, page);
        model.addAttribute("page",page1);
        model.addAttribute("hasPrevious",page1.hasPrevious());
        model.addAttribute("hasNext",page1.hasNext());
        model.addAttribute("types",typeService.list(null));
        return LIST;
    }

    @PostMapping("/blogs/search")
    public String search(@RequestParam(defaultValue = "1") Long pageNo,
                        @RequestParam(defaultValue = "10") Long pageSize,
                        TBlog blog,
                        Model model){
        Page<BlogQuery> page = new Page<>(pageNo,pageSize);
        Page<BlogQuery> page1 = blogService.listBlogs(blog, page);
        model.addAttribute("page",page1);
        model.addAttribute("hasPrevious",page1.hasPrevious());
        model.addAttribute("hasNext",page1.hasNext());
        return "admin/blogs :: blogList";
    }

    /**
     * 跳转到更新/新增页面
     */
    @GetMapping("/blogs/toBlog")
    public String toBlog(Model model){
        model.addAttribute("types",typeService.list(null));
        model.addAttribute("tags",tagService.list(null));
        model.addAttribute("blog",new BlogQuery());
        return INPUT;
    }

    /**
     * 跳转到更新页面
     */
    @GetMapping("/blogs/{id}/toUpdate")
    public String toUpdate(@PathVariable Long id, Model model){
        QueryWrapper<TBlogTag> wrapper = new QueryWrapper();
        wrapper.select("tag_id").eq("blog_id",id);
        BlogQuery blogQuery = new BlogQuery();
        List<TBlogTag> list = blogTagService.list(wrapper);
        String tagIds = MyStringUtils.getTagIds(list);
        blogQuery.setStr(tagIds);
        BeanUtils.copyProperties(blogService.getById(id),blogQuery);
        model.addAttribute("blog",blogQuery);
        model.addAttribute("types",typeService.list(null));
        model.addAttribute("tags",tagService.list(null));
        return INPUT;
    }

    /**
     * 新增或者更新方法
     */
    @PostMapping("/blogs")
    public String insert(BlogQuery blog,
                         HttpSession session,
                         RedirectAttributes attributes,
                         MultipartFile file){

        TUser user = (TUser) session.getAttribute("user");
        int result = blogService.insertBlog(blog,user,file);
        if (result>0){
            attributes.addFlashAttribute("message","成功发布！");
        } else {
            attributes.addFlashAttribute("message","发布失败！");
        }
        return REDIRECT_LIST;
    }

    /**
     * 删除blog
     * @param id
     * @param attributes
     * @return
     */
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,
                         RedirectAttributes attributes){
        boolean result = blogService.deleteBlog(id);
        if (result){
            attributes.addFlashAttribute("message","删除成功");
        } else {
            attributes.addFlashAttribute("message","删除失败");
        }
        return REDIRECT_LIST;
    }

}
